package com.sdcqjy.property.activity;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sdcqjy.property.R;
import com.sdcqjy.property.base.SimpleBaseActivity;
import com.sdcqjy.property.mode.ListMsgMode;
import com.sdcqjy.property.utils.GlideUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import co.senab.photoview.PhotoView;

/**
 * Created by 磁磁帅 on 2018/3/31.
 * 功能：
 */

public class ImageActivity extends SimpleBaseActivity {
    @BindView(R.id.textTitle)
    TextView textTitle;
    @BindView(R.id.textTime)
    TextView textTime;
    @BindView(R.id.textR)
    TextView textR;
    @BindView(R.id.layout1)
    LinearLayout layout1;
    //@BindView(R.id.listView)
    //RecyclerView listView;
    private ListMsgMode mode;
    //private List<String> modeList;
    //private MyAdapter adapter;

    public static void open(Context context, ListMsgMode mode) {
        Intent intent = new Intent(context, ImageActivity.class);
        intent.putExtra("mode", mode);
        context.startActivity(intent);
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image);
        ButterKnife.bind(this);
    }

    protected void asynRun() {
        Intent intent = getIntent();
        mode = intent.getParcelableExtra("mode");
        if (mode == null) {
            finish();
            return;
        }
        if (TextUtils.isEmpty(mode.documentPath) || TextUtils.isEmpty(mode.documentRealName)) {
            textR.setVisibility(View.GONE);
        } else textR.setVisibility(View.VISIBLE);

        Resources res = getResources();
        String fabushijian_ = res.getString(R.string.fabushijian_);

        textTime.setText(fabushijian_ + mode.getTime());
        textTitle.setText(mode.title);
        String[] split = mode.imagePath.split(",");

        if (split.length == 0) return;
        ImageViewHolder imageViewHolder;
        for (int i = 0; i < split.length; i++) {
            imageViewHolder = getImageViewHolder();
            layout1.addView(imageViewHolder.icon);
            GlideUtils.displayOfUrl(getActivity(), imageViewHolder.icon, split[i]);
        }

//        modeList = new ArrayList<>(split.length);
//        modeList.addAll(Arrays.asList(split));
//        adapter = new MyAdapter(getActivity(), modeList);
//        listView.setLayoutManager(new LinearLayoutManager(getActivity()));
//        listView.setAdapter(adapter);
//        GlideUtils.displayOfUrl(getActivity(), icon, split[0]);
//        GlideUtils.displayOfUrl(getActivity(), icon2, split[1]);
//        myThread = new MyThread(icon, mode.imagePath);
//        myThread.start();
    }

    @OnClick(R.id.textR)
    public void onViewClicked() {
        WordActivity.open(getActivity(), mode.documentPath);
    }

//    public static class MyAdapter extends SuperRecyclerAdapter<String> {
//        public MyAdapter(Context context, List<String> modeList) {
//            super(context, modeList);
//        }
//        public int getLayoutId() {
//            return R.layout.item_img_list;
//        }
//        public SuperRecyclerAdapter.ViewHolder<String> getViewHolder(View view) {
//            return new ViewHolder(view);
//        }
//    }

    private ImageViewHolder getImageViewHolder() {
        View inflate = LayoutInflater.from(getActivity()).inflate(R.layout.item_img_list, null);
        return new ImageViewHolder(inflate);
    }

    public static class ImageViewHolder {
        @BindView(R.id.icon)
        PhotoView icon;

        ImageViewHolder(View view) {
            ButterKnife.bind(this, view);
        }

        public void setData(String mode) {
            GlideUtils.displayOfUrl(icon.getContext(), icon, mode);
        }
    }


//    protected void onDestroy() {
//        super.onDestroy();
//        if (myThread != null) myThread.onDestroy();
//    }
//    private MyThread myThread;
//    public static class MyThread extends Thread {
//        private ImageView view;
//        private Bitmap bitmap;
//        private String url;
//        public MyThread(ImageView view, String url) {
//            this.view = view;
//            this.url = url;
//        }
//        public void run() {
//            if (view == null || TitleUtils.isEmpty(url)) return;
//            bitmap = GlideUtils.loadImageSync(view.getContext(), url);
//            //bitmap = GlideUtils.loadImageSyncOver(view.getContext(), url);
//            synchronized (MyThread.class) {
//                if (view != null) {
//                    view.post(new Runnable() {
//                        public void run() {
//                            if (view == null || bitmap == null) return;
//                            view.setImageBitmap(bitmap);
//                        }
//                    });
//                }
//            }
//        }
//        public synchronized void onDestroy() {
//            synchronized (MyThread.class) {
//                view = null;
//                bitmap = null;
//            }
//        }
//    }
}
